home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1998 November / Freeware November 1998.img / dist / fw_emacs.idb / usr / freeware / share / emacs / 19.34 / lisp / text-mode.el.z / text-mode.el
Lisp/Scheme  |  1998-10-27  |  6KB  |  173 lines

  1. ;;; text-mode.el --- text mode, and its idiosyncratic commands.
  2.  
  3. ;; Copyright (C) 1985, 1992, 1994 Free Software Foundation, Inc.
  4.  
  5. ;; Maintainer: FSF
  6.  
  7. ;; This file is part of GNU Emacs.
  8.  
  9. ;; GNU Emacs is free software; you can redistribute it and/or modify
  10. ;; it under the terms of the GNU General Public License as published by
  11. ;; the Free Software Foundation; either version 2, or (at your option)
  12. ;; any later version.
  13.  
  14. ;; GNU Emacs is distributed in the hope that it will be useful,
  15. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17. ;; GNU General Public License for more details.
  18.  
  19. ;; You should have received a copy of the GNU General Public License
  20. ;; along with GNU Emacs; see the file COPYING.  If not, write to the
  21. ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  22. ;; Boston, MA 02111-1307, USA.
  23.  
  24. ;;; Commentary:
  25.  
  26. ;; This package provides the fundamental text mode documented in the
  27. ;; Emacs user's manual.
  28.  
  29. ;;; Code:
  30.  
  31. (defvar text-mode-syntax-table nil
  32.   "Syntax table used while in text mode.")
  33.  
  34. (defvar text-mode-abbrev-table nil
  35.   "Abbrev table used while in text mode.")
  36. (define-abbrev-table 'text-mode-abbrev-table ())
  37.  
  38. (if text-mode-syntax-table
  39.     ()
  40.   (setq text-mode-syntax-table (make-syntax-table))
  41.   (modify-syntax-entry ?\" ".   " text-mode-syntax-table)
  42.   (modify-syntax-entry ?\\ ".   " text-mode-syntax-table)
  43.   (modify-syntax-entry ?' "w   " text-mode-syntax-table))
  44.  
  45. (defvar text-mode-map nil
  46.   "Keymap for Text mode.
  47. Many other modes, such as Mail mode, Outline mode and Indented Text mode,
  48. inherit all the commands defined in this map.")
  49.  
  50. (if text-mode-map
  51.     ()
  52.   (setq text-mode-map (make-sparse-keymap))
  53.   (define-key text-mode-map "\e\t" 'ispell-complete-word)
  54.   (define-key text-mode-map "\t" 'tab-to-tab-stop)
  55.   (define-key text-mode-map "\es" 'center-line)
  56.   (define-key text-mode-map "\eS" 'center-paragraph))
  57.  
  58.  
  59. ;(defun non-saved-text-mode ()
  60. ;  "Like text-mode, but delete auto save file when file is saved for real."
  61. ;  (text-mode)
  62. ;  (make-local-variable 'delete-auto-save-files)
  63. ;  (setq delete-auto-save-files t))
  64.  
  65. (defun text-mode ()
  66.   "Major mode for editing text intended for humans to read.
  67. Special commands:
  68. \\{text-mode-map}
  69. Turning on Text mode calls the value of the variable `text-mode-hook',
  70. if that value is non-nil."
  71.   (interactive)
  72.   (kill-all-local-variables)
  73.   (use-local-map text-mode-map)
  74.   (setq mode-name "Text")
  75.   (setq major-mode 'text-mode)
  76.   (setq local-abbrev-table text-mode-abbrev-table)
  77.   (set-syntax-table text-mode-syntax-table)
  78.   (run-hooks 'text-mode-hook))
  79.  
  80. (defvar indented-text-mode-map ()
  81.   "Keymap for Indented Text mode.
  82. All the commands defined in Text mode are inherited unless overridden.")
  83.  
  84. (if indented-text-mode-map
  85.     ()
  86.   ;; Make different definition for TAB before the one in text-mode-map, but
  87.   ;; share the rest.
  88.   (let ((newmap (make-sparse-keymap)))
  89.     (define-key newmap "\t" 'indent-relative)
  90.     (setq indented-text-mode-map (nconc newmap text-mode-map))))
  91.  
  92. (defun indented-text-mode ()
  93.   "Major mode for editing text with indented paragraphs.
  94. In this mode, paragraphs are delimited only by blank lines.
  95. You can thus get the benefit of adaptive filling
  96.  (see the variable `adaptive-fill-mode').
  97. \\{indented-text-mode-map}
  98. Turning on `indented-text-mode' calls the value of the variable
  99. `text-mode-hook', if that value is non-nil."
  100.   (interactive)
  101.   (kill-all-local-variables)
  102.   (use-local-map text-mode-map)
  103.   (define-abbrev-table 'text-mode-abbrev-table ())
  104.   (setq local-abbrev-table text-mode-abbrev-table)
  105.   (set-syntax-table text-mode-syntax-table)
  106.   (make-local-variable 'indent-line-function)
  107.   (setq indent-line-function 'indent-relative-maybe)
  108.   (make-local-variable 'paragraph-start)
  109.   (setq paragraph-start (concat "$\\|" page-delimiter))
  110.   (make-local-variable 'paragraph-separate)
  111.   (setq paragraph-separate paragraph-start)
  112.   (use-local-map indented-text-mode-map)
  113.   (setq mode-name "Indented Text")
  114.   (setq major-mode 'indented-text-mode)
  115.   (run-hooks 'text-mode-hook 'indented-text-mode-hook))
  116.  
  117. (defun center-paragraph ()
  118.   "Center each nonblank line in the paragraph at or after point.
  119. See `center-line' for more info."
  120.   (interactive)
  121.   (save-excursion
  122.     (forward-paragraph)
  123.     (or (bolp) (newline 1))
  124.     (let ((end (point)))
  125.       (backward-paragraph)
  126.       (center-region (point) end))))
  127.  
  128. (defun center-region (from to)
  129.   "Center each nonblank line starting in the region.
  130. See `center-line' for more info."
  131.   (interactive "r")
  132.   (if (> from to)
  133.       (let ((tem to))
  134.     (setq to from from tem)))
  135.   (save-excursion
  136.     (save-restriction
  137.       (narrow-to-region from to)
  138.       (goto-char from)
  139.       (while (not (eobp))
  140.     (or (save-excursion (skip-chars-forward " \t") (eolp))
  141.         (center-line))
  142.     (forward-line 1)))))
  143.  
  144. (defun center-line (&optional nlines)
  145.   "Center the line point is on, within the width specified by `fill-column'.
  146. This means adjusting the indentation so that it equals
  147. the distance between the end of the text and `fill-column'.
  148. The argument NLINES says how many lines to center."
  149.   (interactive "P")
  150.   (if nlines (setq nlines (prefix-numeric-value nlines)))
  151.   (while (not (eq nlines 0))
  152.     (save-excursion
  153.       (let ((lm (current-left-margin))
  154.         line-length)
  155.     (beginning-of-line)
  156.     (delete-horizontal-space)
  157.     (end-of-line)
  158.     (delete-horizontal-space)
  159.     (setq line-length (current-column))
  160.     (if (> (- fill-column lm line-length) 0)
  161.         (indent-line-to 
  162.          (+ lm (/ (- fill-column lm line-length) 2))))))
  163.     (cond ((null nlines)
  164.        (setq nlines 0))
  165.       ((> nlines 0)
  166.        (setq nlines (1- nlines))
  167.        (forward-line 1))
  168.       ((< nlines 0)
  169.        (setq nlines (1+ nlines))
  170.        (forward-line -1)))))
  171.  
  172. ;;; text-mode.el ends here
  173.